package top.zhangjianyong.tools.reptile;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang.StringUtils;
import top.zhangjianyong.tools.utils.JDBCUtil;

import java.io.File;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.*;

/**
 * @author ： zhangjianyong
 * @date ： 2023/3/23 3:38 PM
 */
public class ReptileContent {
    /*图片*/
    private final static Queue<String> IMG_QUEUE = new LinkedList<>();
    private final static Set<String> IMG_SET = new HashSet<>();
    private static String IMG;

    /*网址*/
    public final static Queue<String> URL_QUEUE = new LinkedList<>();
    /*已访问网页地址*/
    private final static Set<String> URL_SET = new HashSet<>();
    private static String URL;

    /*视频*/
    public final static Queue<String> VIDEO_QUEUE = new LinkedList<>();
    private final static Set<String> VIDEO_SET = new HashSet<>();


    public final static String QUEUE_PATH = "/Users/zhangjianyong/img/reptile_content" + File.separator + "url_queue.txt";
    public final static String SET_PATH = "/Users/zhangjianyong/img/reptile_content" + File.separator + "url_set.txt";


    public static void init() {
        System.out.println("加载地址队列");
        File queueFile = new File(QUEUE_PATH);
        if (queueFile.exists()) {
            List<String> list = FileUtil.readLines(queueFile, Charset.defaultCharset());
            URL_QUEUE.addAll(list);
        }

        File setFile = new File(SET_PATH);
        if (setFile.exists()) {
            List<String> list = FileUtil.readLines(setFile, Charset.defaultCharset());
            URL_SET.addAll(list);
        }

    }

    public static void save() {
        System.out.println("保存地址队列");
        Queue<String> queue = new LinkedList<>();
        queue.add(URL);
        queue.addAll(URL_QUEUE);

        File queueFile = new File(QUEUE_PATH);
        FileUtil.writeLines(queue, queueFile, Charset.defaultCharset());

        File setFile = new File(SET_PATH);
        FileUtil.writeLines(URL_SET, setFile, Charset.defaultCharset());
    }

    /**
     * 添加图片地址
     *
     * @param url
     */
    public static void offerImg(String url) {
        if (StringUtils.isEmpty(url)) {
            return;
        }
        if (IMG_SET.contains(url)) {
            return;
        }
        System.out.println("加入图片地址队列地址：" + url);
        IMG_QUEUE.offer(url);
        IMG_SET.add(url);
        return;
    }

    /**
     * 添加图片地址
     *
     * @param list
     */
    public static void offerImg(List<String> list) {
        list.stream().forEach(ReptileContent::offerImg);
    }

    /**
     * 取出图片地址并移除
     *
     * @return
     */
    public static String pollImg() {
        IMG = IMG_QUEUE.poll();
        return IMG;
    }

    /**
     * 返回图片地址
     *
     * @return
     */
    public static String peekImg() {
        return IMG_QUEUE.peek();
    }

    /**
     * 判断图片是否为空
     *
     * @return
     */
    public static boolean isEmptyImg() {
        return IMG_QUEUE.isEmpty();
    }

    /**
     * 判断图片是否为空
     *
     * @return
     */
    public static boolean isNotEmptyImg() {
        return !isEmptyImg();
    }

    /**
     * 添加网页地址
     *
     * @param url
     * @return
     */
    public static void offerUrl(String url, String from) throws SQLException {
        if (StringUtils.isEmpty(url)) {
            return;
        }
       /* if (URL_SET.contains(url)) {
            return;
        }*/
        if (existUrl(url)) {
            return;
        }
        System.out.println("加入网页地址队列地址：" + url);
        addUrl(url, from);
        //URL_QUEUE.offer(url);
        //URL_SET.add(url);
    }

    /**
     * 添加网页地址
     *
     * @param list
     */
    public static void offerUrl(List<String> list, String from) throws SQLException {
        for (String url : list) {
            offerUrl(url, from);
        }
    }


    /**
     * 取出网页地址并移除
     *
     * @return
     */
    public static String pollUrl() throws SQLException {
        Map<String, Object> map = JDBCUtil.qryObj("select url from reptile_url where state=0 order by create_time limit 1");
        String url = (String) map.get("url");
        return url;
        /*URL = URL_QUEUE.poll();
        return URL;*/
    }

    /**
     * 修改网页状态
     *
     * @param url     网址
     * @param process 状态 0-初始，1-错误，2-成功，3-重定向，4-无法访问
     * @throws SQLException
     */
    public static void visitUrl(String url, int process) throws SQLException {
        JDBCUtil.update("update reptile_url set state=? where url=?", process, url);
    }


    public static void addUrl(String url, String from) throws SQLException {
        JDBCUtil.update("insert into reptile_url(url,from_url,state) values(?,?,?)", url, from, 0);
    }

    public static boolean existUrl(String url) throws SQLException {
        Map<String, Object> map = JDBCUtil.qryObj("select count(1) count from reptile_url where url=?", url);
        Long count = (Long) map.get("count");
        return count > 0;
    }


    public static boolean isEmptyUrl() throws SQLException {
        //return URL_QUEUE.isEmpty();
        Map<String, Object> map = JDBCUtil.qryObj("select count(1) count from reptile_url where state=0");
        Long count = (Long) map.get("count");
        return count == 0;
    }

    public static boolean isNotEmptyUrl() throws SQLException {
        return !isEmptyUrl();
    }

    public static void offerVideo(String url) {
        if (StringUtils.isEmpty(url)) {
            return;
        }
        if (VIDEO_SET.contains(url)) {
            return;
        }
        VIDEO_QUEUE.offer(url);
        VIDEO_SET.add(url);
    }

    public static void offerVideo(List<String> videoUrls) {
        videoUrls.forEach(ReptileContent::offerVideo);
    }

    public static boolean isEmptyVideo() {
        return VIDEO_QUEUE.isEmpty();
    }

    public static boolean isNotEmptyVideo() {
        return !isEmptyVideo();
    }

    public static String pollVideo() {
        return VIDEO_QUEUE.poll();
    }

    public static int updateHead(String url, HtmlUtil.HtmlHead head) throws SQLException {
        String sql = "update reptile_url set title=?,keywords=?,description=? where url=? ";
        return JDBCUtil.update(sql, head.title, head.keywords, head.description, url);
    }
}
